package com.ita.acmsystem.merchant.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.ita.acmsystem.merchant.dao.IFoodsDao;
import com.ita.acmsystem.merchant.entity.Foods;
import com.ita.acmsystem.merchant.entity.Merchant;
import com.ita.acmsystem.merchant.util.DBUtil;

public class FoodsDaoImpl implements IFoodsDao {

	@Override
	public Foods query(String foodId) {
		Foods foods = null;

		String sql = "select * from foods where food_id=?";

		Connection conn = DBUtil.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;

		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, foodId);
			rs = ps.executeQuery();

			if (rs.next()) {
				foods = new Foods();
				foods.setFoodId(rs.getString(1));
				foods.setFoodName(rs.getString(2));
				foods.setFoodPrice(rs.getString(3));
				foods.setFoodPic(rs.getString(4));
				foods.setFoodFoodTypeId(rs.getString(5));
				foods.setMerchantId(rs.getString(6));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DBUtil.free(conn, ps, rs);
		return foods;
	}

	@Override
	public List<Foods> queryAll() {
		List<Foods> list = new ArrayList<Foods>();
		Foods foods = null;

		String sql = "select * from foods";

		Connection conn = DBUtil.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;

		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();

			while (rs.next()) {
				foods = new Foods();
				foods.setFoodId(rs.getString(1));
				foods.setFoodName(rs.getString(2));
				foods.setFoodPrice(rs.getString(3));
				foods.setFoodPic(rs.getString(4));
				foods.setFoodFoodTypeId(rs.getString(5));
				foods.setMerchantId(rs.getString(6));
				list.add(foods);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DBUtil.free(conn, ps, rs);
		return list;
	}

	@Override
	public List<Foods> queryByType(String typeId) {
		List<Foods> list = new ArrayList<Foods>();
		Foods foods = null;

		String sql = "select * from foods where food_type_id=?";

		Connection conn = DBUtil.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;

		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, typeId);
			rs = ps.executeQuery();

			while (rs.next()) {
				foods = new Foods();
				foods.setFoodId(rs.getString(1));
				foods.setFoodName(rs.getString(2));
				foods.setFoodPrice(rs.getString(3));
				foods.setFoodPic(rs.getString(4));
				foods.setFoodFoodTypeId(rs.getString(5));
				foods.setMerchantId(rs.getString(6));
				list.add(foods);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DBUtil.free(conn, ps, rs);
		return list;
	}

	@Override
	public List<Foods> queryByMerchantId(String merchantId) {
		List<Foods> list = new ArrayList<Foods>();
		Foods foods = null;

		String sql = "select * from foods where merchant_id=?";

		Connection conn = DBUtil.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;

		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, merchantId);
			rs = ps.executeQuery();

			while (rs.next()) {
				foods = new Foods();
				foods.setFoodId(rs.getString(1));
				foods.setFoodName(rs.getString(2));
				foods.setFoodPrice(rs.getString(3));
				foods.setFoodPic(rs.getString(4));
				foods.setFoodFoodTypeId(rs.getString(5));
				foods.setMerchantId(rs.getString(6));
				list.add(foods);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DBUtil.free(conn, ps, rs);
		return list;
	}

	@Override
	public Foods persist(Foods foods) {
		Foods result = null;

		String sql = "insert into foods values(?,?,?,?,?,?)";

		Connection conn = DBUtil.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;

		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, foods.getFoodId());
			ps.setString(2, foods.getFoodName());
			ps.setString(3, foods.getFoodPrice());
			ps.setString(4, foods.getFoodPic());
			ps.setString(5, foods.getFoodFoodTypeId());
			ps.setString(6, foods.getMerchantId());
			rs = ps.executeQuery();

			result = foods;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DBUtil.free(conn, ps, rs);
		return result;
	}

	@Override
	public boolean update(Foods foods) {
		boolean result = false;

		String sql = "update foods set food_name=?,food_price=?,food_pic=?,food_type_id=?,merchant_id=? where food_id=?";

		Connection conn = DBUtil.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;

		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, foods.getFoodName());
			ps.setString(2, foods.getFoodPrice());
			ps.setString(3, foods.getFoodPic());
			ps.setString(4, foods.getFoodFoodTypeId());
			ps.setString(5, foods.getMerchantId());
			ps.setString(6, foods.getFoodId());
			rs = ps.executeQuery();

			result = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DBUtil.free(conn, ps, rs);
		return result;
	}

	@Override
	public boolean delete(String foodId) {
		boolean result = false;

		String sql = "delete from foods where food_id=?";

		Connection conn = DBUtil.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;

		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, foodId);
			rs = ps.executeQuery();

			result = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DBUtil.free(conn, ps, rs);
		return result;
	}

}
